package codeTop;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class q179 {
    public String largestNumber(int[] nums) {
        List<String> list = new ArrayList<>();
        for (int num : nums) {
            list.add(String.valueOf(num));
        }
        list.sort((a, b) -> {
            int lcm = lcm(a.length(), b.length());
            for (int i = 0; i<= lcm; i++) {
                if (a.charAt(i%a.length()) > b.charAt(i%b.length())) {
                    return -1;
                }
                if (a.charAt(i%a.length()) < b.charAt(i%b.length())) {
                    return 1;
                }
            }
            return 0;
        });
        if (list.get(0).equals("0")) {
            return "0";
        }
        StringBuilder ret = new StringBuilder();
        for (String s : list) {
            ret.append(s);
        }
        return ret.toString();
    }
    // 最大公约数
    public static int gcd(int a, int b) {
        while (b != 0) {
            int tmp = b;
            b = a % b;
            a = tmp;
        }
        return a;
    }

    // 最小公倍数
    public static int lcm(int a, int b) {
        return a / gcd(a, b) * b; // 先除后乘，防止溢出
    }

    public static void main(String[] args) {
        System.out.println(lcm(12, 18)); // 输出 36
    }
}
